<!--
 *                   ___====-_  _-====___
 *             _--^^^#####//      \\#####^^^--_
 *          _-^##########// (    ) \\##########^-_
 *         -############//  |\^^/|  \\############-
 *       _/############//   (@::@)   \############\_
 *      /#############((     \\//     ))#############\
 *     -###############\\    (oo)    //###############-
 *    -#################\\  / VV \  //#################-
 *   -###################\\/      \//###################-
 *  _#/|##########/\######(   /\   )######/\##########|\#_
 *  |/ |#/\#/\#/\/  \#/\##\  |  |  /##/\#/  \/\#/\#/\#| \|
 *  `  |/  V  V  `   V  \#\| |  | |/#/  V   '  V  V  \|  '
 *     `   `  `      `   / | |  | | \   '      '  '   '
 *                      (  | |  | |  )
 *                     __\ | |  | | /__
 *                    (vvv(VVV)(VVV)vvv)
 * 
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 * 
 *                神兽保佑            永无BUG
 -->

<!--
 * @Author: your name
 * @Date: 2021-12-09 20:32:15
 * @LastEditTime: 2022-01-24 12:21:31
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \web-vue\src\views\login.vue
-->
<template>

    <el-row
      type="flex"
      justify="center"
    >
      <el-col :span="6">
        <div class="grid-content bg-purple-light">
          <h2>欢迎来到管理系统</h2>
          <p>p1</p>
        </div>
      </el-col>
      <el-col :span="1">
        <el-divider direction="vertical"></el-divider>
      </el-col>
      <el-col :lg="7" :span="6">
        <el-form
          :model="loginForm"
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
        >
          <el-form-item label="用户名" prop="username" style="width: 380px;">
            <el-input v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password" style="width: 380px;">
            <el-input v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item label="验证码" prop="code" style="width: 380px;">
            <el-input v-model="loginForm.code" style="width: 172px; float:left;"></el-input>
            <el-image :scr="captchaImg" class="captchaImg"></el-image>
          </el-form-item>
          
          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
            >立即创建</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
</template>
<script>
import loginApi from '@/apis/loginApi.js';
import { v4 as uuidv4 } from 'uuid'
import axios from "../utils/axios";


export default {

  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: '',
        token: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { min: 5, max: 5, message: '长度为 5 个字符', trigger: 'blur' }
        ]
      },
      captchaImg: null
    };
  },
  methods: {
    submitForm(formName) {
      let that = this
      this.$refs[formName].validate((valid) => {
        if (valid) {
          that.$axios.post('/login',this.loginForm).then(res =>{
            const jwt = res.headers['authorization']
            this.$store.commit('SET_TOKEN',jwt)

            this.$router.push("/index")

          })

          alert('submit!');
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    handleSubmit() {
      this.logInfo.uuid = uuidv4()
      var loginPrex = "http://127.0.0.1:8029";
      console.log(this.logInfo)
      loginApi.login(this.logInfo, res => {
        console.log('res')

      });

      const that = this
      axios.post(loginPrex + "/User/login", this.logInfo, res => {

      }).then(function (response) {
        that.$router.push({ path: '/Home' })

      });
    },
    getCaptcha(){
      this.$axios.get('/Captcha').then(res =>{

        console.log('/Captcha')
        console.log(res)
        this.loginForm.token = res.data.data.token
        this.captchaImg = res.data.data.captchaImg
        })
    }

    // Result - code, msg, data
  },
  mounted() {

  },
  created() {
    this.getCaptcha()

  }
}
</script>
<style scoped>
.el-row{
  background-color: #fafafa;
  height: 100%;
  display: flex;
  align-items: center;
  text-align: center;
}
.el-divider{
  height: 200px;
}
.captchaImg{
  float:left;
  margin-left: 8px;
  border-radius: 4px;

}
</style>